const isProduction = process.env.NODE_ENV === 'production'
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
// 修改vue.config.js 分离不常用代码库
module.exports = {
  devServer: {
    proxy: {
      '/self': {
        target: 'http://127.0.0.1:9002', // 后端接口地址
        ws: true,
        changeOrigin: true, // 是否允许跨越
        pathRewrite: {
          '^/self': '/' // 重写,
        }
      },
      '/prod': {
        target: 'https://szzq.yndk.cn:9001', // 后端接口地址
        ws: true,
        changeOrigin: true, // 是否允许跨越
        pathRewrite: {
          '^/prod': '/' // 重写,
        }
      },
      '/mock': {
        target: '/', // 后端接口地址
        ws: true,
        changeOrigin: true, // 是否允许跨越
        pathRewrite: {
          '^/mock': '/mock' // 重写,
        }
      }
    }
  },
  chainWebpack: config => {
    config.plugin('loadshReplace').use(new LodashModuleReplacementPlugin())
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule.use('vue-loader-v16').loader('vue-loader-v16').end()
      .use('vue-svg-loader').loader('vue-svg-loader')
  },
  configureWebpack: config => {
    if (isProduction) {
      // 提出第三方库
      // config.externals = {
      //   moment: 'moment',
      //   lodash: '_'
      // }
      // 开启分离js
      config.optimization = {
        runtimeChunk: 'single',
        splitChunks: {
          chunks: 'all',
          maxInitialRequests: Infinity,
          minSize: 20000,
          cacheGroups: {
            vendor: {
              test: /[\\/]node_modules[\\/]/,
              name (module) {
                // get the name. E.g. node_modules/packageName/not/this/part.js
                // or node_modules/packageName
                const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
                // npm package names are URL-safe, but some servers don't like @ symbols
                return `npm.${packageName.replace('@', '')}`
              }
            }
          }
        }
      }
    }
  },
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          modifyVars: {
            // 'primary-color': '#499DA8'
          },
          javascriptEnabled: true
        }
      }
    }
  },
  productionSourceMap: !isProduction
}
